// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: HPZ Setup Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 40 : TYPE_HPZSETUP

// Function declarations
reserve function HPZSetup_Function97
reserve function HPZSetup_Function98
reserve function HPZSetup_Function99
reserve function HPZSetup_Function100
reserve function HPZSetup_Function101
reserve function HPZSetup_Function102

// Static Values
public value HPZSetup_value33 = 0;
public value HPZSetup_value34 = 0;
public value HPZSetup_value35 = 0;
public value HPZSetup_value36 = 0;

// Tables
public table HPZSetup_table38
	768, 18, 770, 9, 772, 18, 770, 9
end table

public table HPZSetup_table39
	774, 18, 776, 9, 778, 18, 776, 9
end table

public table HPZSetup_table40
	780, 18, 783, 9, 786, 18, 783, 9
end table

public table HPZSetup_table41
	789, 18, 792, 9, 795, 18, 792, 9
end table

public table HPZSetup_table42
	0, 0, 1, 2, 3, 3, 2, 1, 2, 3, 3, 2, 1, 0
end table

function HPZSetup_Function97
	object.value9 = temp1
	object.value9 >>= 16
	object.value10 = temp1
	object.value10 >>= 8
	object.value10 &= 255
	object.value11 = temp1
	object.value11 &= 255
	object.value12 = temp2
	object.value12 >>= 16
	object.value13 = temp2
	object.value13 >>= 8
	object.value13 &= 255
	object.value14 = temp2
	object.value14 &= 255
	if object.value12 == 0
		object.value12 = 1
	end if
	if object.value13 == 0
		object.value13 = 1
	end if
	if object.value14 == 0
		object.value14 = 1
	end if
	object.value9 <<= 8
	object.value10 <<= 8
	object.value11 <<= 8
	object.value9 /= object.value12
	object.value10 /= object.value13
	object.value11 /= object.value14
	if object.value9 == 0
		object.value9 = 256
	end if
	if object.value10 == 0
		object.value10 = 256
	end if
	if object.value11 == 0
		object.value11 = 256
	end if
end function


function HPZSetup_Function98
	temp0 = 0
	GetPaletteEntry(1, 2, temp1)
	SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
	temp0++
	GetPaletteEntry(1, 3, temp1)
	SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
	temp0++
	GetPaletteEntry(1, 4, temp1)
	SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
	temp0++
	GetPaletteEntry(1, 5, temp1)
	SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
	temp0++
	CopyPalette(1, 0, 4, 2, 4)
	while temp0 < 64
		GetTableValue(temp1, temp0, PlayerObject_SonicSuperPal)
		SetPaletteEntry(3, 0, temp1)
		temp0++
		GetTableValue(temp1, temp0, PlayerObject_SonicSuperPal)
		SetPaletteEntry(3, 1, temp1)
		temp0++
		GetTableValue(temp1, temp0, PlayerObject_SonicSuperPal)
		SetPaletteEntry(3, 2, temp1)
		temp0++
		GetTableValue(temp1, temp0, PlayerObject_SonicSuperPal)
		SetPaletteEntry(3, 3, temp1)
		temp0 -= 3
		SetPaletteFade(5, 3, 4, 48, 0, 4)
		GetPaletteEntry(5, 0, temp1)
		SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
		temp0++
		GetPaletteEntry(5, 1, temp1)
		SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
		temp0++
		GetPaletteEntry(5, 2, temp1)
		SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
		temp0++
		GetPaletteEntry(5, 3, temp1)
		SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
		temp0++
	loop
end function


function HPZSetup_Function99
	temp0 = 0
	GetPaletteEntry(1, 13, temp1)
	SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
	temp0++
	GetPaletteEntry(1, 17, temp1)
	SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
	temp0++
	GetPaletteEntry(1, 18, temp1)
	SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
	temp0++
	GetPaletteEntry(1, 16, temp1)
	SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
	temp0++
	while temp0 < 24
		GetPaletteEntry(0, 13, temp1)
		GetPaletteEntry(1, 13, temp2)
		CallFunction(HPZSetup_Function97)
		GetTableValue(temp1, temp0, PlayerObject_TailsSuperPal)
		temp2 = temp1
		temp2 >>= 8
		temp2 &= 255
		temp3 = temp1
		temp3 &= 255
		temp1 >>= 16
		temp1 *= 248
		temp1 /= object.value9
		temp2 *= 248
		temp2 /= object.value10
		temp3 *= 248
		temp3 /= object.value11
		temp1 &= 240
		temp2 &= 240
		temp3 &= 240
		if temp1 > 224
			temp1 = 224
		end if
		if temp2 > 224
			temp2 = 224
		end if
		if temp3 > 224
			temp3 = 224
		end if
		temp1 <<= 16
		temp2 <<= 8
		temp1 += temp2
		temp1 += temp3
		SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
		temp0++
		GetPaletteEntry(0, 17, temp1)
		GetPaletteEntry(1, 17, temp2)
		CallFunction(HPZSetup_Function97)
		GetTableValue(temp1, temp0, PlayerObject_TailsSuperPal)
		temp2 = temp1
		temp2 >>= 8
		temp2 &= 255
		temp3 = temp1
		temp3 &= 255
		temp1 >>= 16
		temp1 *= 256
		temp1 /= object.value9
		temp2 *= 256
		temp2 /= object.value10
		temp3 *= 256
		temp3 /= object.value11
		temp1 &= 240
		temp2 &= 240
		temp3 &= 240
		if temp1 > 224
			temp1 = 224
		end if
		if temp2 > 224
			temp2 = 224
		end if
		if temp3 > 224
			temp3 = 224
		end if
		temp1 <<= 16
		temp2 <<= 8
		temp1 += temp2
		temp1 += temp3
		SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
		temp0++
		GetPaletteEntry(0, 18, temp1)
		GetPaletteEntry(1, 18, temp2)
		CallFunction(HPZSetup_Function97)
		GetTableValue(temp1, temp0, PlayerObject_TailsSuperPal)
		temp2 = temp1
		temp2 >>= 8
		temp2 &= 255
		temp3 = temp1
		temp3 &= 255
		temp1 >>= 16
		temp1 *= 256
		temp1 /= object.value9
		temp2 *= 256
		temp2 /= object.value10
		temp3 *= 256
		temp3 /= object.value11
		temp1 &= 240
		temp2 &= 240
		temp3 &= 240
		if temp1 > 224
			temp1 = 224
		end if
		if temp2 > 224
			temp2 = 224
		end if
		if temp3 > 224
			temp3 = 224
		end if
		temp1 <<= 16
		temp2 <<= 8
		temp1 += temp2
		temp1 += temp3
		SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
		temp0++
		GetPaletteEntry(0, 16, temp1)
		GetPaletteEntry(1, 16, temp2)
		CallFunction(HPZSetup_Function97)
		GetTableValue(temp1, temp0, PlayerObject_TailsSuperPal)
		temp2 = temp1
		temp2 >>= 8
		temp2 &= 255
		temp3 = temp1
		temp3 &= 255
		temp1 >>= 16
		temp1 *= 256
		temp1 /= object.value9
		temp2 *= 256
		temp2 /= object.value10
		temp3 *= 256
		temp3 /= object.value11
		temp1 &= 240
		temp2 &= 240
		temp3 &= 240
		if temp1 > 224
			temp1 = 224
		end if
		if temp2 > 224
			temp2 = 224
		end if
		if temp3 > 224
			temp3 = 224
		end if
		temp1 <<= 16
		temp2 <<= 8
		temp1 += temp2
		temp1 += temp3
		SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
		temp0++
	loop
end function


function HPZSetup_Function100
	temp0 = 0
	GetPaletteEntry(1, 26, temp1)
	SetTableValue(temp1, temp0, PlayerObject_KnuxSuperAltPal)
	temp0++
	GetPaletteEntry(1, 27, temp1)
	SetTableValue(temp1, temp0, PlayerObject_KnuxSuperAltPal)
	temp0++
	GetPaletteEntry(1, 28, temp1)
	SetTableValue(temp1, temp0, PlayerObject_KnuxSuperAltPal)
	temp0++
end function


function HPZSetup_Function101
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			SwapMusicTrack("MysticCave2_F.ogg", 0, 0x3BA8, 0x1F40)
			break
		case 2
			SetMusicTrack("MysticCave2_F.ogg", 0, 0x3BA8)
			SwapMusicTrack("Invincibility_F.ogg", 2, 0x78B1, 0x1F40)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("MysticCave2_F.ogg", 0, 0x3BA8)
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			break
		end switch
	else
		stage.musicFlag = 1
	end if
end function


function HPZSetup_Function102
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			SwapMusicTrack("MysticCave2.ogg", 0, 0x48F0, 0x30D4)
			break
		case 2
			SetMusicTrack("MysticCave2.ogg", 0, 0x48F0)
			SwapMusicTrack("Invincibility.ogg", 2, 0x9717, 0x30D4)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("MysticCave2.ogg", 0, 0x48F0)
			SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			break
		end switch
	else
		stage.musicFlag = 2
	end if
end function


event ObjectMain
	if stage.actNum == 1
		if object[0].xpos > 0xE800000
			stage.newWaterLevel = 0x6140000
		else
			stage.newWaterLevel = 0x74C0000
		end if
	end if
	object.value0++
	if object.value0 == 2
		tileLayer[0].deformationOffsetW++
	end if
	if object.value0 == 4
		tileLayer[0].deformationOffsetW++
		tileLayer[1].deformationOffsetW++
		object.value0 = 0
	end if
	object.value1++
	if object.value1 == 5
		object.value1 = 0
		RotatePalette(0, 185, 188, 1)
	end if
	if object.value2 < 2
		GetTableValue(temp0, object.value3, HPZSetup_table38)
		Copy16x16Tile(738, temp0)
		temp0++
		Copy16x16Tile(739, temp0)
		GetTableValue(temp0, object.value3, HPZSetup_table39)
		Copy16x16Tile(744, temp0)
		temp0++
		Copy16x16Tile(745, temp0)
		GetTableValue(temp0, object.value3, HPZSetup_table40)
		Copy16x16Tile(750, temp0)
		temp0++
		Copy16x16Tile(751, temp0)
		temp0++
		Copy16x16Tile(752, temp0)
		GetTableValue(temp0, object.value3, HPZSetup_table41)
		Copy16x16Tile(759, temp0)
		temp0++
		Copy16x16Tile(760, temp0)
		temp0++
		Copy16x16Tile(761, temp0)
		object.value3++
		GetTableValue(object.value2, object.value3, HPZSetup_table38)
		object.value3++
		object.value3 &= 7
	else
		object.value2--
	end if
	if object.value4 < 2
		GetTableValue(temp0, object.value5, HPZSetup_table38)
		Copy16x16Tile(740, temp0)
		temp0++
		Copy16x16Tile(741, temp0)
		GetTableValue(temp0, object.value5, HPZSetup_table39)
		Copy16x16Tile(746, temp0)
		temp0++
		Copy16x16Tile(747, temp0)
		GetTableValue(temp0, object.value5, HPZSetup_table40)
		Copy16x16Tile(753, temp0)
		temp0++
		Copy16x16Tile(754, temp0)
		temp0++
		Copy16x16Tile(755, temp0)
		GetTableValue(temp0, object.value5, HPZSetup_table41)
		Copy16x16Tile(762, temp0)
		temp0++
		Copy16x16Tile(763, temp0)
		temp0++
		Copy16x16Tile(764, temp0)
		object.value5++
		GetTableValue(object.value4, object.value5, HPZSetup_table38)
		object.value5++
		object.value5 &= 7
	else
		object.value4--
	end if
	if object.value6 < 2
		GetTableValue(temp0, object.value7, HPZSetup_table38)
		Copy16x16Tile(742, temp0)
		temp0++
		Copy16x16Tile(743, temp0)
		GetTableValue(temp0, object.value7, HPZSetup_table39)
		Copy16x16Tile(748, temp0)
		temp0++
		Copy16x16Tile(749, temp0)
		GetTableValue(temp0, object.value7, HPZSetup_table40)
		Copy16x16Tile(756, temp0)
		temp0++
		Copy16x16Tile(757, temp0)
		temp0++
		Copy16x16Tile(758, temp0)
		GetTableValue(temp0, object.value7, HPZSetup_table41)
		Copy16x16Tile(765, temp0)
		temp0++
		Copy16x16Tile(766, temp0)
		temp0++
		Copy16x16Tile(767, temp0)
		object.value7++
		GetTableValue(object.value6, object.value7, HPZSetup_table38)
		object.value7++
		object.value7 &= 7
	else
		object.value6--
	end if
	if HPZSetup_value33 > 0
		HPZSetup_value33--
	end if
	temp0 = object.value8
	temp0 /= 6
	GetTableValue(HPZSetup_value35, temp0, HPZSetup_table42)
	object.value8++
	object.value8 %= 84
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		temp1 = object[currentPlayer].xpos
		temp1 >>= 16
		temp2 = object[currentPlayer].ypos
		temp2 >>= 16
		temp2 += object[currentPlayer].collisionBottom
		temp2--
		if object[currentPlayer].gravity == GRAVITY_GROUND
			Get16x16TileInfo(temp0, temp1, temp2, TILEINFO_ANGLEB)
			if temp0 == 1
				object[currentPlayer].state = PlayerObject_Waterslide
				object[currentPlayer].direction = FLIP_NONE
			end if
		end if
	next
	if object[0].state == PlayerObject_Waterslide
		if object.value16 == 0
			if object.value17 == 0
				PlaySfx(SfxName[Waterfall], 0)
				StopSfx(SfxName[Waterfall Loop])
				object.value17 = 1
			else
				StopSfx(SfxName[Waterfall])
				PlaySfx(SfxName[Waterfall Loop], 0)
			end if
		end if
		object.value16++
		object.value16 &= 63
	else
		if object.value16 != 0
			object.value16++
			object.value16 &= 63
		else
			object.value16 = 0
			object.value17 = 0
		end if
	end if
	if HPZSetup_value36 == 0
		if stage.debugMode == 0
			if options.stageSelectFlag == 0
				if object[30].type == TypeName[Act Finish]
					HPZSetup_value36 = 1
					CallNativeFunction2(SetAchievement, 7, 100)
				end if
			end if
		end if
	end if
end event


event ObjectDraw
	temp0 = stage.waterLevel
	temp0 -= screen.yoffset
	if temp0 < 0
		temp0 = 0
	end if
	if temp0 > screen.ysize
		temp0 = screen.ysize
	end if
	SetActivePalette(0, 0, temp0)
	if HPZSetup_value33 > 0
		SetActivePalette(2, temp0, screen.ysize)
	else
		SetActivePalette(1, temp0, screen.ysize)
	end if
	AddDrawListEntityRef(2, object.entityPos)
end event


event ObjectStartup
	if stage.actNum == 1
		SetMusicTrack("MysticCave2.ogg", 0, 0x48EF)
		SpeedUpMusic = HPZSetup_Function101
		SlowDownMusic = HPZSetup_Function102
	else
		SetMusicTrack("Extra.ogg", 0, 0xE9CC)
	end if
	if stage.player2Enabled == 1
		object[1].xpos = object[0].xpos
	end if
	animalType1 = TypeName[Rocky]
	animalType2 = TypeName[Mocky]
	SetPaletteEntry(0, 192, 0)
	object[10].type = TypeName[HPZ Setup]
	object[10].priority = PRIORITY_ACTIVE
	object[10].drawOrder = 0
	object[10].value5 = 2
	object[10].value7 = 4
	LoadPalette("HPZ_WaterPal.act", 1, 0, 0, 256)
	CallFunction(HPZSetup_Function98)
	CallFunction(HPZSetup_Function99)
	CallFunction(HPZSetup_Function100)
	LoadPalette("ElectricFlash.act", 2, 0, 0, 256)
	HPZSetup_value33 = 0
	HPZSetup_value34 = 0
	if stage.listPos > 20
		stage.listPos = 20
		stage.activeList = REGULAR_STAGE
	endif
	SetLayerDeformation(1, 64, 2, 0, 0, 0)
	temp0 = 0
	while temp0 < 256
		Rand(temp1, 4)
		SetLayerDeformation(3, 16, temp1, 1, temp0, 16)
		temp0 += 16
	loop
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
